package org.example.day20250105;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.example.day20250105.domain.*;
import org.example.day20250105.flink.jdbc.OdsXwDetailDataWriteByFlink;

import java.util.ArrayList;
import java.util.List;

public class ExcelDataHandle {
    private static final String filePath = "src/main/resources/20250105/WW2216069019-循环-90-3-1-20230104110446.xlsx";
    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 1000;
    /**
     * 缓存的数据
     */
    private static List<OdsXwCycleData> odsXwCycleDataList = new ArrayList<>(BATCH_COUNT);
    private static List<OdsXwDetailData> odsXwDetailDataList = new ArrayList<>(BATCH_COUNT);
    private static List<OdsXwStepData> odsXwStepDataList = new ArrayList<>(BATCH_COUNT);
    private static List<OdsTestFlow> odsTestFlowList = new ArrayList<>(BATCH_COUNT);
    private static final OdsTestFlow finalFlow = new OdsTestFlow();

    public static void main(String[] args) {
//        System.out.println("11");
//        ExcelReaderBuilder read = EasyExcel.read("D:\\study\\bigdata-api-study\\file\\20250104\\新威EXCEL样例数据集\\WW2216069019-循环-90-3-1-20230104110446.xlsx",
//                OdsTestFlow.class,
//                new ExcelDataHandle());
//
//        read.sheet(1)
//                .doRead();
//        System.out.println("22");
        EasyExcel.read(filePath, OdsTestFlowDataFormat.class, new OdsTestFlowDataHandle()).sheet(1).doRead();
        System.out.println(odsTestFlowList.add(finalFlow));
        System.out.println(odsTestFlowList);
//        System.out.println(odsXwCycleDataList.size());
//        for (OdsXwCycleData cycleData : odsXwCycleDataList) {
//            System.out.println(cycleData);
//        }


    }

    public static List<OdsXwCycleData> readCycleData() {
        EasyExcel.read(filePath, OdsXwCycleData.class, new OdsXwCycleDataHandle()).sheet(2).doRead();
        return odsXwCycleDataList;
    }

    public static List<OdsTestFlow> readTestFlowData(){
        EasyExcel.read(filePath, OdsTestFlowDataFormat.class, new OdsTestFlowDataHandle()).sheet(1).doRead();
        return odsTestFlowList;
    }
    public static List<OdsXwStepData> readStepData() {
        EasyExcel.read(filePath, OdsXwStepData.class, new OdsXwStepDataHandle()).excelType(ExcelTypeEnum.XLSX).sheet(3).doRead();
        return odsXwStepDataList;
    }

    public static List<OdsXwDetailData> readDetailData() {
        EasyExcel.read(filePath, OdsXwDetailData.class, new OdsXwDetailDataHandle()).sheet(4).doRead();
        return odsXwDetailDataList;
    }

    /**
     * 新威原始测试流程数据   sheet 1
     */
    public static class OdsTestFlowDataHandle implements ReadListener<OdsTestFlowDataFormat> {
        private static int rowCount = 0;

        @Override
        public void invoke(OdsTestFlowDataFormat format, AnalysisContext analysisContext) {
            if (rowCount > 5) {
                return;
            }
            if (rowCount == 0) {
                finalFlow.setStartStepNo(format.getV1());
                finalFlow.setVoltageUpperLimit(format.getV2());
                finalFlow.setBatteryBatchNumber(format.getV3());
            } else if (rowCount == 1) {
                finalFlow.setCycleTimes(format.getV1());
                finalFlow.setVoltageLowerLimit(format.getV2());
                finalFlow.setCreator(format.getV3());
            } else if (rowCount == 2) {
                finalFlow.setRecordCondition(format.getV1());
                finalFlow.setCurrentUpperLimit(format.getV2());
                finalFlow.setRemark(format.getV3());
            } else if (rowCount == 3) {
                finalFlow.setVoltageRange(format.getV1());
                finalFlow.setCurrentLowerLimit(format.getV2());
            } else if (rowCount == 4) {
                finalFlow.setCurrentRange(format.getV1());
                finalFlow.setStartDatetime(format.getV2());
                finalFlow.setTestBarcode(format.getV3());
            } else if (rowCount == 5) {
                finalFlow.setActiveSubstances(format.getV1());
                finalFlow.setNominalCapacity(format.getV2());
            }
            rowCount++;
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            odsTestFlowList.add(finalFlow);
        }
    }

    /**
     * 循环数据处理   sheet 2
     */
    public static class OdsXwCycleDataHandle implements ReadListener<OdsXwCycleData> {
        @Override
        public void invoke(OdsXwCycleData odsXwCycleData, AnalysisContext analysisContext) {
            odsXwCycleDataList.add(odsXwCycleData);
//            if (odsXwCycleDataList.size() >= BATCH_COUNT) {
//                // 存储完成清理 list
//                odsXwCycleDataList.clear();
//            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }
    }

    /**
     * 新威原始详情数据 sheet 4
     */
    public static class OdsXwDetailDataHandle implements ReadListener<OdsXwDetailData> {
        @Override
        public void invoke(OdsXwDetailData odsXwDetailData, AnalysisContext analysisContext) {
            odsXwDetailDataList.add(odsXwDetailData);
            if (odsXwDetailDataList.size() >= BATCH_COUNT) {
                //每1000条去调用存储方法一次
                OdsXwDetailDataWriteByFlink.save(odsXwDetailDataList);
                // 存储完成清理 list
                odsXwDetailDataList.clear();
            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }
    }

    /**
     * 新威原始工步数据 sheet 3
     */
    public static class OdsXwStepDataHandle implements ReadListener<OdsXwStepData> {
        @Override
        public void invoke(OdsXwStepData odsXwStepData, AnalysisContext analysisContext) {
            odsXwStepDataList.add(odsXwStepData);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }
    }
}
